﻿package com.tudou.home.newskin.buttons
{
    import com.tudou.consts.*;
    import com.tudou.cookie.*;
    import com.tudou.home.newskin.events.*;
    import com.tudou.skin.*;
    import com.tudou.skin.common.button.*;
    import com.tudou.skin.common.events.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class ExpandButton extends Sprite implements IExpand
    {
        private var isExpand:Boolean = false;
        private var hasClicked:Boolean = false;
        private var showStatus:int = 0;
        private var button:BaseButton;
        private var timeOutId:uint = 0;

        public function ExpandButton()
        {
            button = new BaseButton();
            button.setBackground(RectDisplayUtil.getShape(36, 24, 65484, 0));
            button.addStateButton(new WideScreenBTN());
            button.addStateButton(new NormalScreenBTN());
            button.addRollOverOut(new RollOverOutSprite(this.width, this.height));
            button.clickContainer = true;
            addChild(button);
            button.activeIndex = 0;
            button.addEventListener(ButtonEvent.CLICK, clickHlr);
            hasClicked = PlayerSO.getInst().getUsed(CookieConsts.WIDE_SCREEN);
            return;
        }// end function

        private function timeOutHlr() : void
        {
            clearTimeout(timeOutId);
            showStatus = 2;
            processVisible();
            return;
        }// end function

        private function processVisible() : void
        {
            return;
        }// end function

        private function clickCloseHlr(event:MouseEvent) : void
        {
            processClick();
            return;
        }// end function

        private function clickHlr(event:ButtonEvent) : void
        {
            processClick();
            dispatchEvent(new SkinEvent(SkinEvent.EXIT_FULLSCREEN));
            if (event.clickIndex == 0)
            {
                dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.EXPAND + "|1"));
                dispatchEvent(new SkinEvent(SkinEvent.SET_EXPAND, true));
            }
            else
            {
                dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.EXPAND + "|0"));
                dispatchEvent(new SkinEvent(SkinEvent.SET_EXPAND, false));
            }
            return;
        }// end function

        override public function get height() : Number
        {
            return button.height;
        }// end function

        public function setExpand(param1:Boolean) : void
        {
            isExpand = param1;
            button.activeIndex = isExpand ? (1) : (0);
            processVisible();
            return;
        }// end function

        override public function get width() : Number
        {
            return button.width;
        }// end function

        private function processClick() : void
        {
            if (!hasClicked)
            {
                hasClicked = true;
                PlayerSO.getInst().setUsed(CookieConsts.WIDE_SCREEN, hasClicked);
                if (showStatus == 1)
                {
                    clearTimeout(timeOutId);
                    showStatus = 2;
                    processVisible();
                }
            }
            return;
        }// end function

        public function show() : void
        {
            if (showStatus == 0 && !isExpand && !hasClicked)
            {
                showStatus = 1;
                processVisible();
                timeOutId = setTimeout(timeOutHlr, 5000);
            }
            return;
        }// end function

    }
}
